<template>
    <div style="text-align: center;margin-top: 100px;margin-left: 600px">
        <el-card style="width: 400px;">
            <div slot="header" class="clearfix">
                <span>{{admin.name}}</span>
            </div>
            <div>
                <div>
                    <el-upload
                            class="upload-demo"
                            :on-success="chengong"
                            :on-error="shibai"
                            :headers="headers"
                            multiple
                            :data="adminID"
                            :show-file-list="false"
                            action="/updatePic">
                        <img title="点击修改用户头像" :src="admin.photo" style="width: 100px;height: 100px;border-radius: 50px"
                             alt="">
                        <div slot="tip" class="el-upload__tip">点击更换头像！</div>
                    </el-upload>
                </div>
                <div style="margin: 5px 0px">
                    <span>电话号码：</span>
                    <el-tag type="success">{{admin.phone}}</el-tag>
                </div>
                <div style="margin: 5px 0px">
                    <span>家庭号码：</span>
                    <el-tag type="success">{{admin.telephone}}</el-tag>
                </div>
                <div style="margin: 5px 0px">
                    <span>联系地址：</span>
                    <el-tag type="success">{{admin.address}}</el-tag>
                </div>
                <div style="margin: 5px 0px">
                    <span>备注：</span>
                    <el-tag type="success">{{admin.remark}}</el-tag>
                </div>
                <div style="margin: 5px 0px">
                    <span>用户标签：</span>
                    <el-tag v-for="(r, index) in admin.roles" :key="index">{{r.roleName}}</el-tag>
                </div>
                <div style="margin: 5px 0px">
                    <el-button type="success" @click="showUpdateInfo">修改信息</el-button>
                    <el-button type="warning" @click="showPass">修改密码</el-button>
                </div>
            </div>
        </el-card>
        <el-dialog
                title="修改用户信息"
                :visible.sync="dialogVisible"

                width="20%">
            <div>
                <table>
                    <tr>
                        <td>用户名称：</td>
                        <td>
                            <el-input v-model="admin2.name"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>电话号码：</td>
                        <td>
                            <el-input v-model="admin2.phone"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>家庭号码：</td>
                        <td>
                            <el-input v-model="admin2.telephone"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>联系地址：</td>
                        <td>
                            <el-input type="textarea" v-model="admin2.address"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>备注：</td>
                        <td>
                            <el-input v-model="admin2.remark"></el-input>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="updateAdmin">确 定</el-button>
      </span>
        </el-dialog>
        <el-dialog
                title="修改密码"
                :visible.sync="dialogVisible2"

                width="20%">
            <div>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                         class="demo-ruleForm">
                    <el-form-item label="旧密码" prop="oldPass">
                        <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="Pass">
                        <el-input type="password" v-model="ruleForm.Pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">重 置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'AdminInfo',
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                } else if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass')
                    callback()
                } else if (value === this.ruleForm.oldPass) {
                    callback(new Error('新密码与旧密码一致!'))
                }
            }
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'))
                } else if (value !== this.ruleForm.Pass) {
                    callback(new Error('两次输入密码不一致!'))
                } else {
                    callback()
                }
            }
            return {
                admin: [],
                admin2: [],
                adminID:{
                    adminID:""
                },
                /* 设置请求头和token信息 */
                headers: {
                    Authorization: window.sessionStorage.getItem('tokenStr')
                },
                dialogVisible: false,
                dialogVisible2: false,
                ruleForm: {
                    oldPass: '',
                    Pass: '',
                    checkPass: ''
                },
                rules: {
                    oldPass: [
                        {required: true, message: '请输入旧密码', trigger: 'blur'}
                    ],
                    Pass: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPass: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                }
            }
        },
        mounted() {
            this.initAdmin();
        },
        methods: {
            // updatePassword () {
            //     this.$refs.ruleForm.validate((valid) => {
            //         if (valid) {
            //             if (this.ruleForm.oldPass !== this.ruleForm.Pass) {
            //                 this.ruleForm.adminId = this.admin.id
            //                 this.putRequest('/admin/pass', this.ruleForm)
            //                     .then(res => {
            //                         if (res) {
            //                             this.postRequest('/logout')
            //                             window.sessionStorage.removeItem('user')
            //                             window.sessionStorage.removeItem('tokenStr')
            //                             this.$store.commit('initRoutes', [])
            //                             this.$router.replace('/')
            //                         }
            //                     })
            //             } else {
            //                 this.$message({
            //                     type: 'error',
            //                     message: '新密码与旧密码一致！'
            //                 })
            //                 // this.dialogVisible2 = false
            //             }
            //         }
            //     })
            // },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.ruleForm.adminId = this.admin.id;
                        this.putRequest('/admin/pass', this.ruleForm).then(resp => {
                            if (resp) {
                                this.postRequest('/logout');
                                window.sessionStorage.removeItem('user');
                                window.sessionStorage.removeItem('tokenStr');
                                this.$store.commit('initRoutes', []);
                                this.$router.replace('/')
                            }
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '新密码与旧密码一致！'
                        })
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields()
            },
            showPass() {
                this.dialogVisible2 = true
            },
            updateAdmin() {
                if (JSON.stringify(this.admin) !== JSON.stringify(this.admin2)) {
                    if (this.admin2.name !== null) {
                        this.putRequest('/admin/info', this.admin2).then(res => {
                            if (res) {
                                this.dialogVisible = false;
                                this.initAdmin();
                            }
                        })
                    }
                }

            },
            showUpdateInfo() {
                this.admin2 = Object.assign({}, this.admin)
                this.dialogVisible = true
            },


            chengong() {
                this.$message({
                    type: 'success',
                    message: '上传成功！'
                })
                this.initAdmin()
            },
            shibai() {
                this.$message({
                    type: 'error',
                    message: '上传失败！'
                })
            },
            initAdmin() {
                this.getRequest('/admin/info').then(res => {
                    if (res) {
                        this.admin = res;
                        this.adminID.adminID = this.admin.id;
                        this.admin = Object.assign({}, this.admin);
                        window.sessionStorage.setItem('user', JSON.stringify(res))
                        this.$store.commit('INIT_ADMINS', res)
                    }
                })
            }
        }
    }
</script>
<style scoped>

</style>